package com.mixsmart.web.interceptor;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
import org.springframework.web.servlet.view.RedirectView;

import com.mixsmart.utils.StringUtils;

/**
 * 异常处理者
 * @version 1.0
 * @since JDK1.6以上
 * @author lmq
 * <br />
 * 2016年7月24日
 */
public class MixWebExceptionHandler extends AbstractHandlerExceptionResolver {

	@Override
	protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		ModelAndView modelView = new ModelAndView();
		String msg = ex.getMessage();
		if(StringUtils.isNotEmpty(msg)) {
			try {
				msg = URLEncoder.encode(msg, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		modelView.getModelMap().put("msg", msg);
		View view = new RedirectView("/exception/json.do", true, true, true);
		modelView.setView(view);
		return modelView;
	}
}
